package com.yidingdui.controller;


import com.yidingdui.domain.dto.UserLoginDTO;
import com.yidingdui.domain.dto.UserRegisterDTO;
import com.yidingdui.domain.vo.Resultdata;
import com.yidingdui.domain.vo.UserVO;
import com.yidingdui.service.IUserService;
import com.yidingdui.domain.dto.ChangePasswordDTO;
import com.yidingdui.util.UserContext;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {

    private final IUserService IUserService;


    /**
     * 登录
     * @param loginDTO
     * @return
     */
    @PostMapping("login")
    public Resultdata<UserVO> login(@RequestBody @Validated UserLoginDTO loginDTO){
        return Resultdata.success(IUserService.login(loginDTO));
    }

    /**
     * 注册
     */
    @PostMapping("register")
    public Resultdata<UserVO> register(@RequestBody @Validated UserRegisterDTO registerDTO) {
        return IUserService.register(registerDTO);
    }

    /**
     * 获取当前登录用户信息
     */
    @GetMapping("me")
    public Resultdata<UserVO> me() {
        Long userId = UserContext.getUser();
        return Resultdata.success(IUserService.getCurrentUserInfo(userId));
    }

    /**
     * 修改当前用户密码
     */
    @PutMapping("password")
    public Resultdata<Void> changePassword(@RequestBody @Validated ChangePasswordDTO dto) {
        Long userId = UserContext.getUser();
        IUserService.changePassword(userId, dto);
        return Resultdata.success(null);
    }
}

